package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.StudentAtSchool;
import com.xmy.cultivate.entity.StudentAtSchoolScoreDivide;
import com.xmy.cultivate.feign.erp_admin.IAdminFeignClient;
import com.xmy.cultivate.service.IStudentAtSchoolScoreDivideService;
import com.xmy.cultivate.service.IStudentAtSchoolService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/studentAtSchool")
@Api("学生所在学校控制器")
public class StudentAtSchoolController {

    IAdminFeignClient iAdminFeignClient;

    @Autowired
    IStudentAtSchoolService studentAtSchoolService;

    @Autowired
    IStudentAtSchoolScoreDivideService iStudentAtSchoolScoreDivideService;

//    @GetMapping("allSchool")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "name" , value = "学校名" , required = false , dataType = "String"),
//            @ApiImplicitParam(name = "pageNum" , value = "分页数" , required = true, dataType = "String"),
//            @ApiImplicitParam(name = "regional" , value = "所在区域" , required = true , dataType = "String"),
//            @ApiImplicitParam(name = "pageSize" , value = "分页尺寸" , required = true, dataType = "String"),
//            @ApiImplicitParam(name = "gradeSection" , value = "学段" , required = true, dataType = "String"),
//    })
//    @ApiOperation("分页查询所有学校信息")
//    public Result allSchool(@RequestParam Map reMap,
//                            @RequestParam(value = "gradeSection",required = false,defaultValue = "") String gradeSection,
//                            @RequestParam(value = "yearClassId",required = false,defaultValue = "") String yearClassId){
//        return iAdminFeignClient.studentAtSchoolAllSchool(reMap,gradeSection,yearClassId);
//    }


    @GetMapping("allSchool")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" , value = "学校名" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "pageNum" , value = "分页数" , required = true, dataType = "String"),
            @ApiImplicitParam(name = "regional" , value = "所在区域" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "pageSize" , value = "分页尺寸" , required = true, dataType = "String"),
            @ApiImplicitParam(name = "gradeSection" , value = "学段" , required = true, dataType = "String"),
    })
    @ApiOperation("分页查询所有学校信息")
    public Result allSchool(@RequestParam Map reMap,
                            @RequestParam(value = "gradeSection",required = false,defaultValue = "") String gradeSection,
                            @RequestParam(value = "yearClassId",required = false,defaultValue = "") String yearClassId){
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));
        String name = (String) reMap.get("name");
        String regional = (String) reMap.get("regional");
        if (StringUtils.isBlank(gradeSection) && StringUtils.isNotBlank(yearClassId)){
            gradeSection = CommonUtil.getGradeSectionForYearClassId(Integer.parseInt(yearClassId)).toString();
        }

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(regional),"regional",regional);
        queryWrapper.eq(StringUtils.isNotBlank(gradeSection),"grade_section",gradeSection);
        queryWrapper.like(StringUtils.isNotBlank(name) , "name",name);
        queryWrapper.orderByDesc("id");
        IPage<StudentAtSchool> iPage = new Page<>(pageNum,pageSize);
        studentAtSchoolService.allStudentScore(iPage,queryWrapper);
        for (StudentAtSchool studentAtSchool:iPage.getRecords()){
            QueryWrapper<StudentAtSchoolScoreDivide> sQueryWrapper = new QueryWrapper<>();
            sQueryWrapper.eq("student_at_school_id",studentAtSchool.getId());
            sQueryWrapper.eq(StringUtils.isNotBlank(gradeSection),"grade_section",gradeSection);
            sQueryWrapper.orderByAsc("score");
            List<StudentAtSchoolScoreDivide> studentAtSchoolScoreDivideList = iStudentAtSchoolScoreDivideService.list(sQueryWrapper);
            studentAtSchool.setScoreDivideList(studentAtSchoolScoreDivideList);
        }

        return new Result(Code.OK,iPage,"学校信息遍历完成~");
    }
}
